/***************************************************************************
 *            camera.c
 *
 *  Mon Aug 25 15:46:17 2008
 *  Copyright  2008  Tiago Falcão
 *  <developer@tiagofalcao.com>
 ****************************************************************************/

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include "camera.h"

// Inicializa Posicao da Camera
void cam_init(){
	// Posicao da Camera em Esferica
	camera.from_esf.r=5.0;
	camera.from_esf.theta=0.0;
	camera.from_esf.phi=M_PI_2;
	// Vetor "Superior" em Esferica
	camera.upper_esf.r=1.0;
	camera.upper_esf.theta=0.0;
	camera.upper_esf.phi=0.0;
	// Ponto de "Foco" em cartesiana
	camera.to.x=0.0;
	camera.to.y=0.0;
	camera.to.z=0.0;

	cam_update();
}

// Calcula posição cartesiana da camera
void cam_update(){
	camera.from=point_esf2cart(camera.from_esf);
	camera.upper=point_esf2cart(camera.upper_esf);
	camera.from.x+=camera.to.x;
	camera.from.y+=camera.to.y;
	camera.from.z+=camera.to.z;
}

// Posiciona da Camera
void cam_setup(){
		// Especifica sistema de coordenadas de projeção
		glMatrixMode(GL_PROJECTION);
		// Inicializa sistema de coordenadas de projeção
		glLoadIdentity();
		// Especifica a projeção perspectiva
		gluPerspective(20.0,camera.aspect,0.1,500);
		// Especifica sistema de coordenadas do modelo
		glMatrixMode(GL_MODELVIEW);
		// Inicializa sistema de coordenadas do modelo
		glLoadIdentity();
		// Especifica posição do observador e do alvo
		gluLookAt(camera.from.x,camera.from.y,camera.from.z,camera.to.x,camera.to.y,camera.to.z,camera.upper.x,camera.upper.y,camera.upper.z);
}

//Seta Correcao de Aspecto
void cam_aspect(GLfloat aspect){
	camera.aspect=aspect;
}

// Move camera no eixo X
void cam_trans_x(short int signal){
	camera.to.x+=signal*.1;
	cam_update();
}
// Move camera no eixo Y
void cam_trans_y(short int signal){
	camera.to.y+=signal*.1;
	cam_update();
}
// Move camera no eixo Z
void cam_trans_z(short int signal){
	camera.to.z+=signal*.1;
	cam_update();
}

// Rotaciona Camera com Theta
void cam_rot_theta(short int signal){
	camera.from_esf.theta+=M_PI_8*signal;
	camera.upper_esf.theta=camera.from_esf.theta;
	cam_update();
}

// Rotaciona Camera com Phi
void cam_rot_phi(short int signal){
	camera.from_esf.phi+=M_PI_8*signal;
	camera.upper_esf.phi+=M_PI_8*signal;
	cam_update();
}

// Distacia da Camera
void cam_zoom(short int signal){
	camera.from_esf.r+=signal*1.0;
	if(camera.from_esf.r<1.)
		camera.from_esf.r=1.;
	cam_update();
}


